/**
 * @file 红包相关 hook
 */
import { appEvents, eventBus } from '@/app/app-events';
import { getWatchCore } from '@/core/watch-sdk';
import { useInteractReceiveStore } from '@/store/use-interact-receive-store';
import { computed, onBeforeMount, onBeforeUnmount, ref } from 'vue';

/**
 * @hook 互动-红包状态
 * */
export const useRedpackStatus = () => {
  /** 设置某个红包的状态 */
  const setRedpackStatus = (redpackId: string, status: string) => {
    eventBus.$emit(appEvents.interaction.SetRedpackStatus, {
      redpackId,
      status,
    });
  };

  return {
    setRedpackStatus,
  };
};

/**
 * @hook 互动-红包
 */
export const useRedpack = () => {
  const interactReceiveStore = useInteractReceiveStore();

  const watchCore = getWatchCore();
  /** 红包 SDK 实例 */
  const redpackSdk = watchCore.interactReceive.getRedpack();

  /** 自定义红包皮肤 */
  const customImgConfig = computed(() => {
    return {
      redpackCoverImg: interactReceiveStore.interactSetting.redpackImage,
    };
  });

  /** 当前显示的红包数据 */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const redpackData = ref<UniversalParams<any>>();

  /** 打开某个红包 */
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  function openRedpack(data: UniversalParams<any>) {
    if (data.type.includes('rain')) {
      // 类型是红包雨，则发送事件打开红包雨
      eventBus.$emit(appEvents.interaction.OpenRedpackRain, {
        redCacheId: data.redCacheId,
        redpackId: data.redpackId,
        avatar: data.user.pic,
        nickname: data.user.nick,
        greeting: data.content,
        type: data.type,
      });
    } else {
      redpackData.value = data;
    }
  }

  onBeforeMount(() => {
    eventBus.$on(appEvents.interaction.OpenRedpack, openRedpack);
  });

  onBeforeUnmount(() => {
    eventBus.$off(appEvents.interaction.OpenRedpack, openRedpack);
  });

  const { setRedpackStatus } = useRedpackStatus();
  /** 处理红包状态改变 */
  function onStatusChange(redpackId: string, status: string) {
    setRedpackStatus(redpackId, status);
  }

  /** 处理关闭红包 */
  function onCloseRedpack() {
    redpackData.value = undefined;
  }

  /** 处理点击提现按钮 */
  function onClickWithdraw() {
    redpackData.value = undefined;
    eventBus.$emit(appEvents.withdraw.OpenWithdrawApply, true);
  }

  /** 处理点击积分记录 */
  function onClickScoreRecord() {
    redpackData.value = undefined;
    eventBus.$emit(appEvents.interaction.OpenScoreRecord, true);
  }

  /** 处理口令红包领取 */
  function onPasswordReceived(pwd: string) {
    // 发送口令到聊天室
    pwd && eventBus.$emit(appEvents.chat.SendMessageToChat, pwd);
  }

  return {
    redpackSdk,
    redpackData,
    customImgConfig,
    openRedpack,
    onStatusChange,
    onCloseRedpack,
    onClickWithdraw,
    onClickScoreRecord,
    onPasswordReceived,
  };
};
